package com.banmao.youZone.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AesUtil {

    public static final String CODE_TYPE_AES = "AES";
    private static final String CipherMode_5 = "AES/ECB/PKCS5Padding";
    public static final String CipherMode_7 = "AES/ECB/PKCS7Padding";

    public static final String SIGN_KEY = "light-app-123456";

    private static int parse(char c) {
        return c >= 'a' ? ((c - 'a') + 10) & 15 : c >= 'A' ? ((c - 'A') + 10) & 15 : (c - '0') & 15;
    }

    public static String decrypt(String str, String str2) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes(StandardCharsets.UTF_8), CipherMode_5);
        Cipher cipher = Cipher.getInstance(CipherMode_5);
        cipher.init(2, secretKeySpec);

        Base64 base64 = new Base64();
        byte[] decode = base64.decode(str);
        return new String(cipher.doFinal(decode));
//        return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(str)));
    }

    public static String aesEncode(String str, String str2) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(str.getBytes(), CODE_TYPE_AES);
            Cipher cipher = Cipher.getInstance(CipherMode_5);
            cipher.init(1, secretKeySpec);
            return parseByte2HexStr(cipher.doFinal(str2.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String aesDecode(String str, String str2) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(str.getBytes(StandardCharsets.UTF_8), CODE_TYPE_AES);
            Cipher cipher = Cipher.getInstance(CipherMode_5);
            cipher.init(2, secretKeySpec);
            return new String(cipher.doFinal(HexString2Bytes(str2)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String parseByte2HexStr(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bArr) {
            String hexString = Integer.toHexString(b & 255);
            if (hexString.length() == 1) {
                hexString = '0' + hexString;
            }
            stringBuffer.append(hexString.toUpperCase());
        }
        return stringBuffer.toString();
    }

    public static byte[] HexString2Bytes(String str) {
        byte[] bArr = new byte[str.length() / 2];
        int i = 0;
        int i2 = 0;
        while (i < bArr.length) {
            int i3 = i2 + 1;
            int i4 = i3 + 1;
            bArr[i] = (byte) ((parse(str.charAt(i2)) << 4) | parse(str.charAt(i3)));
            i++;
            i2 = i4;
        }
        return bArr;
    }

    public static String decryptToUTF8String(String str, String str2) {
        if (str2 == null) {
            str2 = "";
        }
        try {
            byte[] decode = Base64Utils.decode(str);
            SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes(StandardCharsets.UTF_8), CODE_TYPE_AES);
            Cipher cipher = Cipher.getInstance(CipherMode_5);
            cipher.init(2, secretKeySpec);
            byte[] doFinal = cipher.doFinal(decode);
            if (doFinal == null) {
                return null;
            }
            return new String(doFinal, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static String encrypt(String str, String str2, String str3) {
//        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
//            return "";
//        }
//        try {
//            SecretKeySpec secretKeySpec = new SecretKeySpec(str.getBytes(), CODE_TYPE_AES);
//            Cipher cipher = Cipher.getInstance(str3);
//            cipher.init(1, secretKeySpec);
//            return android.util.Base64.encodeToString(cipher.doFinal(str2.getBytes(StandardCharsets.UTF_8)), 0);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }
//
//    public static String decrypt(String str, String str2, String str3) {
//        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
//            return "";
//        }
//        try {
//            SecretKeySpec secretKeySpec = new SecretKeySpec(str.getBytes(), CODE_TYPE_AES);
//            Cipher cipher = Cipher.getInstance(str3);
//            cipher.init(2, secretKeySpec);
//            return new String(cipher.doFinal(android.util.Base64.decode(str2, 0)), StandardCharsets.UTF_8);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }
}
